package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.cust.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.cust.*;
import com.miplus.ccrm.core.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 2-2.客户及工单服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/cust")
public class CustController {
    @Resource
    private ConCustomerService conCustomerService;
    @Resource
    private ConContactService conContactService;
    @Resource
    private ConAddressService conAddressService;
    @Resource
    private ConCasesService conCasesService;
    @Resource
    private ConCaseprocessService conCaseprocessService;
    @Resource
    private JCommonData jCommonData;

    /**
     * 取客户信息LIST
     */
    @PostMapping("/customerQuery")
    public R<PageData<CustResVo>> customerQuery(@RequestBody @Validated CustReqVo custReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conCustomerService.customerQuery(user,custReqVo));
    }

    /**
     * 取客户信息One
     */
    @GetMapping("/customerInfo")
    public R<CustResVo> customerInfo(@RequestParam @NotNull String customernumber){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(conCustomerService.customerInfo(user,customernumber));
    }

    /**
     * 通过电话取客户信息One
     */
    @GetMapping("/customerInfoByPhone")
    public R<CustResVo> customerInfoByPhone(@RequestParam @NotNull String custphone){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(conCustomerService.customerInfoByPhone(user,custphone));
    }

    /**
     * 更新客户标签
     */
    @PostMapping("/updateCustTags")
    public R<CmdResVo> updateTags(@RequestBody @Validated CustTagReqVo custTagReqVo){
        return R.Success(conCustomerService.updateTags(custTagReqVo));
    }

    /**
     * 取客户对应联系人LIST
     */
    @GetMapping("/contactList")
    public R<PageData<ContResVo>> contactList(@Validated ContReqVo contReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conContactService.contactList(user,contReqVo));
    }

    /**
     * 取客户对应联系人One
     */
    @GetMapping("/contactInfo")
    public R<ContResVo> contactInfo(@RequestParam @NotNull int contactnumber){
        return R.Success(conContactService.contactInfo(contactnumber));
    }

    /**
     * 添加客户联系人
     */
    @PostMapping("/contactInsert")
    public R<CmdResVo> contactInsert(@RequestBody @Validated ContEditVo contEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conContactService.insertContact(usrid,contEditVo));
    }

    /**
     * 修改客户联系人
     */
    @PostMapping("/contactUpdate")
    public R<CmdResVo> updateContact(@RequestBody @Validated ContEditVo contEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conContactService.updateContact(usrid,contEditVo));
    }

    /**
     * 删除客户联系人
     */
    @PostMapping("/contactDelete")
    public R<CmdResVo> contactDelete(@RequestBody @Validated ContDelVo contDelVo) {
        return R.Success(conContactService.deleteContact(contDelVo.getContactnumber()));
    }

    /**
     * 取客户对应地址LIST
     */
    @GetMapping("/addressList")
    public R<PageData<AddrResVo>> addressList(@Validated AddrReqVo addrReqVo){
        return R.SuccessPage(conAddressService.addressList(addrReqVo));
    }

    /**
     * 取客户对应地址One
     */
    @GetMapping("/addressInfo")
    public R<AddrResVo> addressInfo(@RequestParam @NotNull int addressnumber){
        return R.Success(conAddressService.getAddressInfo(addressnumber));
    }

    /**
     * 添加客户地址
     */
    @PostMapping("/addressInsert")
    public R<CmdResVo> addressInsert(@RequestBody @Validated AddrEditVo addrEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conAddressService.insertAddress(usrid,addrEditVo));
    }

    /**
     * 修改客户地址
     */
    @PostMapping("/addressUpdate")
    public R<CmdResVo> addressUpdate(@RequestBody @Validated AddrEditVo addrEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conAddressService.updateAddress(usrid,addrEditVo));
    }

    /**
     * 删除客户地址
     */
    @PostMapping("/addressDelete")
    public R<CmdResVo> addressDelete(@RequestBody @Validated AddrDelVo addrDelVo) {
        return R.Success(conAddressService.deleteAddress(addrDelVo.getAddressnumber()));
    }

    /**
     * 取工单LIST
     * 有LIST参数，使用POST
     */
    @PostMapping("/caseList")
    public R<PageData<CaseResVo>> caseList(@RequestBody @Validated CaseReqVo caseReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conCasesService.caseList(user,caseReqVo));
    }

    /**
     * 取工单One
     */
    @GetMapping("/caseInfo")
    public R<CaseResVo> caseInfo(@RequestParam @NotNull Integer casenumber){
        return R.Success(conCasesService.caseInfo(casenumber));
    }

    /**
     * 添加客户
     */
    @PostMapping("/custInsert")
    public R<CmdResVo> custInsert(@RequestBody @Validated CustAddVo custAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCustomerService.custInsert(usrid,custAddVo));
    }

    /**
     * 修改客户
     */
    @PostMapping("/custUpdate")
    public R<CmdResVo> custUpdate(@RequestBody @Validated CustEditVo custEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCustomerService.custUpdate(usrid,custEditVo));
    }

    /**
     * 添加工单
     */
    @PostMapping("/caseInsert")
    public R<CmdResVo> caseInsert(@RequestBody @Validated CaseAddVo caseAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.caseInsert(usrid,caseAddVo));
    }

    /**
     * 取工单进程LIST
     */
    @GetMapping("/caseProcessList")
    public R<List<CaseProcResVo>> caseProcessList(@RequestParam @NotNull Integer casenumber){
        return R.Success(conCaseprocessService.caseProcessList(casenumber));
    }

    /**
     * 工单处理
     */
    @PostMapping("/caseProcessAdd")
    public R<CmdResVo> caseProcessAdd(@RequestBody @Validated CaseProcAddVo caseProcAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.caseProcessAdd(usrid,caseProcAddVo));
    }

    /**
     * 取工单处理部门（责任部门）LIST
     */
    @GetMapping("/getXbDistGrpList")
    public R<List<LabelResVo>> getXbDistGrpList(){
        return R.Success(conCasesService.getXbDistGrpList());
    }

    /**
     * 取安全电话号码
     */
    @GetMapping("/getSecuPhone")
    public R<CmdResVo> getSecuPhone(@RequestParam @NotNull Integer contactnumber){
        return R.Success(conContactService.getSecuPhone(contactnumber));
    }

    /**
     * 批量工单处理
     */
    @PostMapping("/batchProcessAdd")
    public R<CmdResVo> batchProcessAdd(@RequestBody @Validated CaseBatchVo batchVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.batchProcessAdd(usrid,batchVo));
    }
}
